package cn.com.xjnt.vas.ordering.domain.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.google.common.collect.Lists;

import cn.com.xjnt.vas.manage.domain.entity.Organization;
import cn.com.xjnt.vas.manage.domain.entity.Region;

/**
 * The persistent class for the ordering_hospital database table.
 * 
 */
@Entity
@Table(name = "ordering_hospital")
@NamedQuery(name = "Hospital.findAll", query = "SELECT h FROM Hospital h")
public class Hospital extends com.frameworks.core.entity.IdEntity {
    private static final long serialVersionUID = 1L;

    private String address;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;

    private String introduction;

    private int level;

    private String name;
    @ManyToOne
    @JoinColumn(name="organization_id")
    private Organization organization;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;

    // bi-directional many-to-one association to Region
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "province")
    private Region province;

    // bi-directional many-to-one association to Region
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "city")
    private Region city;

    // bi-directional many-to-one association to Region
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "district")
    private Region district;
    
    // bi-directional many-to-one association to Department
    @OneToMany(mappedBy = "hospital", cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, orphanRemoval = true)
    private List<Department> departments = Lists.newArrayList();
    
    // bi-directional many-to-one association to HospitalSeller
    @OneToMany(mappedBy = "hospital", cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, orphanRemoval = true)
    private List<FoodSeller> foodSellers = Lists.newArrayList();

    // bi-directional many-to-one association to News
    @OneToMany(mappedBy = "hospital", cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, orphanRemoval = true)
    private List<News> news = Lists.newArrayList();
    
    // bi-directional many-to-one association to News
    @OneToMany(mappedBy = "hospital", cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, orphanRemoval = true)
    private List<Notice> notices = Lists.newArrayList();

    // bi-directional many-to-one association to Ward
    @OneToMany(mappedBy = "hospital", cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, orphanRemoval = true)
    private List<Ward> wards = Lists.newArrayList();

    public Hospital() {
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getIntroduction() {
        return this.introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public int getLevel() {
        return this.level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getUpdateTime() {
        return this.updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Region getProvince() {
        return province;
    }

    public void setProvince(Region province) {
        this.province = province;
    }

    public Region getCity() {
        return city;
    }

    public void setCity(Region city) {
        this.city = city;
    }

    public Region getDistrict() {
        return district;
    }

    public void setDistrict(Region district) {
        this.district = district;
    }

    public List<Department> getDepartments() {
        return this.departments;
    }

    public void setDepartments(List<Department> departments) {
        this.departments = departments;
    }

    public Department addDepartment(Department department) {
        getDepartments().add(department);
        department.setHospital(this);

        return department;
    }

    public Department removeDepartment(Department department) {
        getDepartments().remove(department);
        department.setHospital(null);

        return department;
    }
    
    public List<FoodSeller> getFoodSellers() {
        return this.foodSellers;
    }

    public void setFoodSellers(List<FoodSeller> foodSellers) {
        this.foodSellers = foodSellers;
    }

    public FoodSeller addFoodSeller(FoodSeller foodSeller) {
        getFoodSellers().add(foodSeller);
        foodSeller.setHospital(this);

        return foodSeller;
    }

    public FoodSeller removeFoodSeller(FoodSeller foodSeller) {
        getFoodSellers().remove(foodSeller);
        foodSeller.setHospital(null);

        return foodSeller;
    }

    public List<News> getNews() {
        return this.news;
    }

    public void setNews(List<News> news) {
        this.news = news;
    }

    public News addNews(News news) {
        getNews().add(news);
        news.setHospital(this);

        return news;
    }

    public News removeNews(News news) {
        getNews().remove(news);
        news.setHospital(null);

        return news;
    }

    public List<Notice> getNotices() {
        return notices;
    }

    public void setNotices(List<Notice> notices) {
        this.notices = notices;
    }

    public Notice addNotice(Notice notice) {
        getNotices().add(notice);
        notice.setHospital(this);

        return notice;
    }

    public Notice removeNotice(Notice notice) {
        getNotices().remove(notice);
        notice.setHospital(null);

        return notice;
    }
    public List<Ward> getWards() {
        return this.wards;
    }

    public void setWards(List<Ward> wards) {
        this.wards = wards;
    }

    public Ward addWard(Ward ward) {
        getWards().add(ward);
        ward.setHospital(this);

        return ward;
    }

    public Ward removeWard(Ward ward) {
        getWards().remove(ward);
        ward.setHospital(null);

        return ward;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    @PrePersist
    public void onCreate() {
        this.createTime = this.updateTime = new Date();
    }

    @PreUpdate
    public void onUpdate() {
        this.updateTime = new Date();
    }
}